import Vue from 'vue'
import VueRouter from 'vue-router'
import BrowsePage from "@/views/BrowsePage/BrowsePage.vue";

Vue.use(VueRouter)

const routes = [
  {

    path: "/",
    name: "BrowsePage",
    component: BrowsePage
  },
  {
    path: "/BrowsePage",
    name: "BrowsePage",
    component: () => import("@/views/BrowsePage/BrowsePage.vue"),
  },
  {
    path: "/login",
    name: "Login",
    component: () => import("@/views/login/index.vue")
  },
  {
    path: "/register",
    name: "Register",
    component: () => import("@/views/register/index.vue")
  },
  {
    path: "/aboutUs",
    name: "关于我们",
    component: () => import("@/views/aboutUs/index.vue"),
  },
  {
    path:"/information",
    name:"个人资料",
    component: () => import("@/views/information/index.vue"),
  },
  {
    path: '/Main/information/tipdetail',
    name: "帖子内容",
    component: () => import("@/views/information/detail/detail.vue"),
  },
  {
    path: "/login/forgetPassword",
    name: "忘记密码",
    component: () => import("@/views/login/forgetPassword.vue")
  },
  {
    path: "/Main",
    name: "Main",
    component: () => import("@/views/MainPage.vue"),
    meta: { requiresAuth: true },
    children: [
      {
        path: "home",
        name: "主页",
        component: () => import("@/views/home/index.vue"),
        meta: { requiresAuth: true },
      },
      {
        path: "forum",
        name: "魔王论坛",
        component: () => import("@/views/forum/index.vue"),
        meta: { requiresAuth: true },
      },
      {
        path: 'aigc',
        component: { template: '<router-view />' },
        meta: { requiresAuth: true },
        children: [
          {
            path: '',
            name: 'AI应用平台',
            component: () => import('../views/aIGC/index.vue'),
            meta: { requiresAuth: true }
          },
          {
            path: 'config/:id',
            name: 'AIGCConfig',
            component: () => import('../views/aIGC/config.vue'),
            meta: { requiresAuth: true }
          },
          {
            path: 'knowledge-base',
            name: 'KnowledgeBase',
            component: () => import('../views/aIGC/KnowledgeBase.vue'),
            meta: { requiresAuth: true }
          }
        ]
      },
      {
        path: 'viewEdit',
        name: 'layout',
        component: () => import('../views/viewedit/index.vue'),
        meta: { requiresAuth: true },
      },
      {
        path: "forum/detail",
        name: "帖子内容",
        component: () => import("@/views/forum/detail/detail.vue"),
        meta: { requiresAuth: true },
      },
      {
        path: "forum/publish",
        name: "发布话题",
        component: () => import("@/views/forum/publish/publish.vue"),
        meta: { requiresAuth: true },
      },
      {
        path: "groupChats",
        name: "世界频道",
        component: () => import("@/views/groupChats/index.vue"),
        meta: { requiresAuth: true },
      },
      {
        path: "littleGroupChats",
        name: "群聊",
        component: () => import("@/views/littleGroupChats/index.vue"),
        meta: { requiresAuth: true },
        children: [
          {
            path: "chat",
            name: "群聊聊天",
            component: () => import("@/views/littleGroupChats/chat.vue"),
            meta: { requiresAuth: true },
          }
        ]
      },

       {
         path: "privateChat",
         name: "私聊",
         component: () => import("@/views/privateChat/index.vue"),
         meta: { requiresAuth: true },
         children: [
           {
             path: "chat",
             name: "聊天",
             component: () => import("@/views/privateChat/chat.vue"),
             meta: { requiresAuth: true },
           },
         ],
       },
       {
         path: "friendsList",
         name: "好友列表",
         component: () => import("@/views/friendsList/index.vue"),
         meta: { requiresAuth: true },
       },
       {
         path: "bulletinBoard",
         name: "公告栏",
         component: () => import("@/views/bulletinBoard/index.vue"),
         meta: { requiresAuth: true },
       },
       {
         path: "customerService",
         name: "智能客服",
         component: () => import("@/views/customerService/index.vue"),
         meta: { requiresAuth: true },
       },
       {
         path: "aboutUs",
         name: "关于我们",
         component: () => import("@/views/aboutUs/index.vue"),
         meta: { requiresAuth: true },
       },
       {path: "tipRank",
         name: "帖子排行榜",
         component: () => import("@/views/TipByRank/index.vue"),
         meta: { requiresAuth: true },
       }
    ],

  },
  {path: "/admin/adminPage",
    name: "admin",
    component: () => import("@/adminviews/MainPage.vue"),
    meta: { requiresAuth: true },
    children: [
      {
        path: "",
        name: "用户管理",
        component: () =>import("@/adminviews/adminPage/index.vue")
      },
      {
        path: "adminForum",
        name: "帖子管理",
        component: () =>import("@/adminviews/adminForum/index.vue")
      },
      {
        path: "adminBulletin",
        name: "公告管理",
        component: () =>import("@/adminviews/adminBulletin/index.vue")
      },
      {
        path: "adminDepartment",
        name: "学院管理",
        component: () =>import("@/adminviews/adminDepartment/index.vue")
      },
      {
        path: "adminGroup",
        name: "群聊管理",
        component: () =>import("@/adminviews/adminGroup/index.vue")
      },
      {
        path: "adminBulletin/publish",
        name: "发布公告",
        component: () => import("@/adminviews/adminBulletin/publish/publish.vue"),
      },
      {
        path: "adminDepartment/createDept",
        name: "创建学院",
        component: () => import("@/adminviews/adminDepartment/createDept/createDept.vue"),
      },
    ]},
]

const router = new VueRouter({
  routes
})

// 解决NavigationDuplicated错误
// Vue Router 3.1.0+版本会在重复导航时抛出NavigationDuplicated错误
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => {
    if (err.name !== 'NavigationDuplicated') throw err;
  });
};

const originalReplace = VueRouter.prototype.replace;
VueRouter.prototype.replace = function replace(location) {
  return originalReplace.call(this, location).catch(err => {
    if (err.name !== 'NavigationDuplicated') throw err;
  });
};

// 全局路由守卫
import store from '@/store'

router.beforeEach((to, from, next) => {
  // 检查路由是否需要认证
  if (to.matched.some(record => record.meta.requiresAuth)) {
    // 检查用户是否已登录
    if (!store.getters.isLoggedIn) {
      // 未登录，重定向到登录页面
      next({
        path: '/login',
        query: { redirect: to.fullPath } // 保存原本要访问的路径，登录后可以重定向回去
      })
    } else {
      // 已登录，正常访问
      next()
    }
  } else {
    // 不需要认证的路由，正常访问
    next()
  }
})

export default router
